/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-11-17 11:08:54
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-11-28 09:24:42
 * @FilePath: \blog\routes\admin\login\login.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { User } = require('../../../model/user.js');
// 密码加密：引用bcrypt第三方模块
const bcrypt = require('bcryptjs');

module.exports = async(req, res) => {
    // 获取post参数
    // console.log(req.fields);
    // 查询用户是否存在
    let user = await User.findOne({ account: req.fields.username});
    if (user) {
        let isValidate = await bcrypt.compare(req.fields.password, user.password);
        if (isValidate) {
          res.send({ 'code': 0,"data":{"token":"1111token23324","userInfo":{"id":user._id,"username":req.fields.username}}});
        } else {
          res.send({ 'code': 1 });
        }
    } else {
        // 没有查询到用户
        res.send({ 'code': 1 });
    }
}